package com.mqtt.ctwingmqtt.controller;

import com.mqtt.ctwingmqtt.domain.ResponseResult;
import com.mqtt.ctwingmqtt.domain.dto.DeviceDto;
import com.mqtt.ctwingmqtt.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(value = "设备接口")
@RestController
@Slf4j
@RequestMapping("/device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;
    @ApiOperation("添加设备")
    @PostMapping("/add")
    public ResponseResult add(@RequestBody DeviceDto deviceDto){
        log.info("添加设备");
        Boolean add = deviceService.add(deviceDto);
        if (add){
            return ResponseResult.success("添加成功");
        }else {
            return ResponseResult.error("添加失败");
        }
    }
    @ApiOperation("批量添加")
    @PostMapping("/addAll")
    public ResponseResult addAll(@RequestBody MultipartFile file){
        log.info("批量添加");
        try {
            ResponseResult<String> result = deviceService.addAll(file);
            return result;
        } catch (Exception e) {
            return ResponseResult.error("添加失败");
        }
    }
    @ApiOperation("删除设备")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam Long id){
        log.info("删除设备");
        Boolean delete = deviceService.delete(id);
        if (delete){
            return ResponseResult.success("删除成功");
        }else {
            return ResponseResult.error("删除失败");
        }
    }
    @ApiOperation("批量删除")
    @PostMapping("/deleteAll")
    public ResponseResult deleteAll(@RequestParam String ids){
        log.info("批量删除");
        String[] split = ids.split(",");
        for (String s : split) {
            Long id = Long.parseLong(s);
            Boolean delete = deviceService.delete(id);
            if (!delete){
                return ResponseResult.error("删除失败");
            }
        }
        return ResponseResult.success("删除成功");
    }
    @ApiOperation("修改设备")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody DeviceDto deviceDto){
        log.info("修改设备");
        Boolean update = deviceService.update(deviceDto);
        if (update){
            return ResponseResult.success("修改成功");
        }else {
            return ResponseResult.error("修改失败");
        }
    }
    @ApiOperation("查询全部设备")
    @GetMapping("/selectAll")
    public ResponseResult selectAll(){
        log.info("查询全部设备");
        return ResponseResult.success(deviceService.selectAll());
    }
    @ApiOperation("查询单个设备")
    @GetMapping("/selectOne")
    public ResponseResult selectOne(@RequestParam Long id){
        log.info("查询单个设备");
        return ResponseResult.success(deviceService.selectOne(id));
    }
    @ApiOperation("条件查询")
    @PostMapping("/selectPage")
    public ResponseResult selectPage(@RequestBody(required = false) DeviceDto deviceDto){
        log.info("条件查询");
        if(deviceDto==null){
            deviceDto = new DeviceDto();
        }
        return ResponseResult.success(deviceService.selectPage(deviceDto));
    }

}
